import { createStore, applyMiddleware } from 'redux';
import reducer from './reducer';
import reduxLogger from 'redux-logger'; // 控制台输出日志
import reduxThunk from 'redux-thunk'; // 实现异步方式1
import reduxPromise from 'redux-promise'; // 实现异步方式2

const store = createStore(
    reducer,
    applyMiddleware(reduxLogger, reduxThunk, reduxPromise)
);
export default store;

// taskAction
const taskAction = {
    // 基于reduxPromise中间件，可以像日常写请求一样，中间件帮我们处理了
    async getTaskList() {
        let res;
        try {
            res = await applyMiddleware.getList();
        }catch(error) {
            console.log('error')
        }
        return {
            type: 'TASK_LIST',
            payload: res
        }
    },
    // redux-thunk 中间件写法
    getTaskThunk() {
        return async dispatch => {
            try {
                let res = api.getList();
                dispatch({
                    type: 'TASK_LIST',
                    payload: res
                })
            }catch(err) {
                dispatch({
                    type: 'TASK_LIST',
                    payload: []
                })
            }
        }
    }
}
